package com.chee.plant.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.chee.plant.hibernate.object.User;
import com.chee.plant.server.HibernateSessionFactory;

/**
 * 对应好友列表UserFriend的数据库操作
 * 
 * @author xue cb
 * 
 */
public class UserFriendDAO extends BaseHibernateDAO {
	/**
	 * 根据用户Id获取用户所有的好友，并根据好友当前种植级别从大到小排序
	 * 
	 * @param userId
	 *            用户Id
	 * @return 用户好友列表
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUserFriendByOwnerId(Integer userId) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select user from User as user,UserFriend as uf where uf.FriendId=user.id and uf.OwnerId=:ownerId order by user.CropLevel desc";
		Query query = session.createQuery(hql);
		query.setInteger("ownerId", userId);
		List<User> list = query.list();
		return list;
	}

	/**
	 * 主方法，用于测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 1,测试getUserFriendByOwnerId(Integer userId)
		List<User> list = new UserFriendDAO().getUserFriendByOwnerId(2);
		System.out.println(list.size());
		for (int i = 0; i < list.size(); i++) {
			User user = (User) list.get(i);
			System.out.println(user.getEmail());
			System.out.println(user.getCropLevel());
		}
		System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");

	}

}
